package com.suntown.util;
import java.sql.*;

public class ToolMySQL {
    private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";                                                  //驱动器
    private static String PORT = "3306";                                                                                         //端口号
    private static String HOSTNAME = "localhost";                                                                                     //地址
    private static String DATABASE = "";                                                            //数据库名称
    private static String USER;                                                                                         //用户名
    private static String PASS;                                                                                         //密码
    private static Connection connection = null;
    private static Statement statement = null;

    public static class ConnectResult{
        public boolean result;
    }

    public static int StartMySQL(){
        String DB_URL = "jdbc:mysql://" + HOSTNAME + ":" + PORT + "/" + DATABASE;
        try {
            Class.forName(JDBC_DRIVER);                                                                                 //加载JDBC驱动程序
            connection = DriverManager.getConnection(DB_URL,USER,PASS);                                                 //建立数据库连接
            statement = connection.createStatement();                                                                   //实例化Statement对象,执行sql语句
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("无法加载驱动程序",e);
        } catch (SQLException e) {
            //throw new RuntimeException("无法连接到数据库",e);
            if(e instanceof com.mysql.cj.jdbc.exceptions.CommunicationsException){
                return -1;
            }
            return 0;
        }

        return 1;
    }

    public static boolean StartMySQL(String db,String sql){
        Connection conn = null;
        Statement st = null;
        String DB_URL = "jdbc:mysql://" + HOSTNAME + ":" + PORT + "/" + db;
        boolean result = false;
        try {
            Class.forName(JDBC_DRIVER);                                                                                 //加载JDBC驱动程序
            conn = DriverManager.getConnection(DB_URL,USER,PASS);                                                 //建立数据库连接
            st = conn.createStatement();
            //实例化Statement对象,执行sql语句
            int effectRows = st.executeUpdate(sql);
            result = effectRows > 0;
        }catch (SQLException e) {

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(st!=null){
                try{
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        return result;
    }

    //建立数据库连接
    public static void EndMySQL(){
        try {
            if (ToolMySQL.statement != null) {
                ToolMySQL.statement.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException("关闭Statement失败",e);
        }
        try {
            if (ToolMySQL.connection != null) {
                ToolMySQL.connection.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException("关闭connection失败",e);
        }
    }                                                                                                                   //关闭数据库连接
    public static Boolean ExecuteSQLNotResult(String sql){
        try {
            return statement.execute(sql);                                                                              //执行sql语句（返回是否成功）
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }                                                                                                                   //执行无结果的sql语句（建表）
    public static ResultSet ExecuteSQLWithResult(String sql){
        try {
            return statement.executeQuery(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }                                                                                                                   //执行有结果的sql语句（查询）
    public static void setPASS(String pass) {
        PASS = pass;
    }
    public static void setUSER(String user) {
        USER = user;
    }
    public static void setDATABASE(String database) {
        DATABASE = database;
    }
    public static void setHOSTNAME(String hostname) {
        HOSTNAME = hostname;
    }
    public static void setPORT(String port) {
        PORT = port;
    }
    public static void setAll(String port,String hostname,String database,String user,String pass){
        ToolMySQL.PORT = port;
        ToolMySQL.HOSTNAME = hostname;
        ToolMySQL.DATABASE = database;
        ToolMySQL.USER = user;
        ToolMySQL.PASS = pass;
    }
}